package com.lej.my.space

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.lej.core.base.BaseModel
import com.lej.core.base.BaseViewModel
import com.lej.dataservice.club.bean.main.my.MySpaceInfo
import com.lej.dataservice.club.http.checkData
import com.lej.my.collect.LoadEmpty
import com.lej.my.collect.LoadError
import com.lej.my.collect.LoadLoading
import com.lej.my.collect.LoadSuccess
import com.lej.my.http.myApiService
import kotlinx.coroutines.launch

/**
 *  author: cjq
 *  date  : 2022/6/9 0009
 *  desc  :
 */
class MySpaceViewModel : BaseViewModel<BaseModel>() {
    var spaceInfo: MutableLiveData<MutableList<MySpaceInfo>> = MutableLiveData()
    var loadingState: MutableLiveData<Int> = MutableLiveData(0)

    fun getSpaceInfo(result: (List<MySpaceInfo>) -> Unit) {
        viewModelScope.launch {
            loadingState.value = LoadLoading
            kotlin.runCatching {
                val info = myApiService.getMySpace().checkData()
                info.list?.let {
                    loadingState.value = if (it.isEmpty()) LoadEmpty else LoadSuccess
                    result.invoke(it)
                    return@runCatching
                }
                loadingState.value = LoadError
            }.onFailure {
                loadingState.value = LoadError
            }
        }
    }
}